package com.test.mchelepesa.dialog_rice;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.mchelepesa.R;
import com.test.mchelepesa.bean_rice.config_info_rice.AppShowInfo_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class BindChannelDialog_rice extends Dialog {

    private final String mobile;
    private final String oldChannel;
    private final AppShowInfo_rice.WalletChannel channel;

    @SuppressLint("SetTextI18n")
    public BindChannelDialog_rice(@NonNull Context context, String mobile, String oldChannel, AppShowInfo_rice.WalletChannel channel) {
        super(context);
        this.mobile = mobile;
        this.oldChannel = oldChannel;
        this.channel = channel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bind_channel_rice);

        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setCanceledOnTouchOutside(false);

        TextView descTv = findViewById(R.id.bind_channel_tv_desc);
        ImageView logoIv = findViewById(R.id.bind_channel_iv_logo);
        TextView channelTv = findViewById(R.id.bind_channel_tv_channel);
        TextView mobileTv = findViewById(R.id.bind_channel_tv_mobile);

        findViewById(R.id.bind_channel_dont_do).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_BIND_CHANNEL_DONT_DO_CLICK");
            dismiss();
        });

        if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(oldChannel) || channel == null) return;

        descTv.setText(getContext().getString(R.string.bind_channel_desc_1,oldChannel));

        String logoUrl = channel.getLogo();
        if (!TextUtils.isEmpty(logoUrl)){
            new Thread(() -> {
                try {
                    URL url = new URL(logoUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(4995);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        new Handler(Looper.getMainLooper()).post(() -> logoIv.setImageBitmap(bitmap));
                    }
                }catch (Exception e){
                    StringUtil_rice.printException(e);
                }
            }).start();
        }

        channelTv.setText(channel.getDesc());
        mobileTv.setText(mobile);

        findViewById(R.id.bind_channel_add).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_BIND_CHANNEL_ADD_CLICK");
            dismiss();
            HttpManager_rice.getInstance().bindBankAccount(mobile,mobile,channel.getValue());
        });

    }


}
